<?php

namespace App\Repository\Orders;

use App\Models\Orders\OrdersLogisticsModel as model;
use App\Repository\BaseRepository;
use App\Repository\Product\ProductRepository;

class OrdersLogisticsRepository extends BaseRepository
{
    private $model;
    private $productModel;

    public function __construct()
    {
        $this->model = new model;
        $this->productModel = new ProductRepository;
        parent::__construct();
    }

    //列表
    public function index($where)
    {
        $cron = $where;
        $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
        // $cron['where'] = $where;
        // var_dump($cron,111111111);
        $data = $this->search($this->model, $cron);
        return $data;
    }

    public function getTimeline($number)
    {
        $content = $this->model->where(['express_num' => $number])->value('text');
        if (empty($content)) {
            return [];
        }
        $content = json_decode($content, true);
        $rows = [];
        foreach ($content as $key => $val) {
            $rows[] = [
                'content' => $val['context'],
                'time' => $val['time']
            ];
        }
        return $rows;
    }

    //添加
    public function add($data)
    {
        //必填 字段
        $mustField = [
            'express_name',
            'express_num'
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'express_name' => '快递名称不能为空',
            'express_num' => '快递单号不能为空'
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        if (!empty($insertData['text']) && is_array($insertData['text'])) {
            $insertData['text'] = json_encode($insertData['text']);
        }
        // 判断是否存在  如果存在 直接修改
        $isTrue = $this->getOne(['express_num' => $insertData['express_num']]);
        if ($isTrue) {
            if (empty($insertData)) {
                return $this->returnMsg(103, '快递信息为空');
            }
            $isTrueRes = $this->model->where(['express_num' => $insertData['express_num']])->update(['text' => $insertData['text']]);
            return $this->returnMsg(200, '修改成功');
        }
        $insertData['created_at'] = date('Y-m-d H:i:s');

        $res = $this->model->insertGetId($insertData);
        if ($res) {
            return $this->returnMsg(200, '上传成功');
        }
        return $this->returnMsg(1, '上传失败');
    }

    //删除
    public function del($where)
    {
        $flag = true;
        $msg = '未知错误';
        $code = 1;
        if (empty($where)) {
            $flag = false;
            $msg = '删除条件不能为空';
            $code = 2;
        }
        if ($flag) {
            $res = $this->model->where($where)->get();
            if (!$res) {
                $flag = false;
                $msg = '未查询到数据';
                $code = 3;
            }
        }
        if ($flag) {
            $del = $this->model->where($where)->delete();
            if ($del) {
                $msg = '删除成功';
                $code = 200;
            }
        }
        return $this->returnMsg($code, $msg);
    }

    //修改
    public function edit($where, $data, $isMust = false)
    {
        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        //过滤字段
        //过滤字段
        $optionalFields = $this->model->getTableField();
        $field = $this->getRealData($data, [], $optionalFields, []);

        //先查询是否存在
        $res = $this->model->where($where)->update($field);
        if ($isMust && $res) {
            return $this->returnMsg(200, '修改成功');
        }
        if (!$isMust) {
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(3, '请重新修改');
    }

    //列表查询
    public function lists($where = [], $page = 1, $limit = 10)
    {
        $res = $this->search($this->model, $where, $page, $limit);

        return $res;
    }

    public function getOne($where, $field = ['*'])
    {
        return $this->model->where($where)->select($field)->first();
    }
}
